package person.twj.securitysessionredis;

import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.userdetails.User;
import org.springframework.session.Session;
import org.springframework.session.data.redis.RedisSessionRepository;
import org.springframework.session.web.http.SessionRepositoryFilter;

import java.util.Set;


/**
 * 获取所有用户session信息，打印输出用户名
 * 这是用于线上管理用户状态的
 */
@Slf4j
public class TestMain {
    public static void main(String[] args) {


        RedisTemplate redisTemplate = createRedisTemplate();
        RedisSessionRepository repository = new RedisSessionRepository(redisTemplate);

        Set<String> keys = redisTemplate.keys("spring:session:sessions:*");
        keys.forEach(key->{
            String sessionId = key.substring("spring:session:sessions:".length());
            Session session = repository.findById(sessionId);
            SecurityContext context = session.getAttribute("SPRING_SECURITY_CONTEXT");
            User user = (User) context.getAuthentication().getPrincipal();
            log.info("输出 {}",user.getUsername());
        });



    }
    public static RedisTemplate<String, Object> createRedisTemplate() {
        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
        redisTemplate.setKeySerializer(RedisSerializer.string());
        redisTemplate.setHashKeySerializer(RedisSerializer.string());
        if (getDefaultRedisSerializer() != null) {
            redisTemplate.setDefaultSerializer(getDefaultRedisSerializer());
        }
        redisTemplate.setConnectionFactory(getRedisConnectionFactory());
        redisTemplate.setBeanClassLoader(TestMain.class.getClassLoader());
        redisTemplate.afterPropertiesSet();
        return redisTemplate;
    }

    public static RedisConnectionFactory getRedisConnectionFactory() {
        RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration();
        redisStandaloneConfiguration.setDatabase(1);
        redisStandaloneConfiguration.setPassword("123456");
        redisStandaloneConfiguration.setPort(6379);
        redisStandaloneConfiguration.setHostName("localhost");

        LettuceConnectionFactory lettuceConnectionFactory = new LettuceConnectionFactory(redisStandaloneConfiguration);

        lettuceConnectionFactory.start();
        return lettuceConnectionFactory;
    }

    /**
     * 使用Java序列化
     * @return
     */
    public static RedisSerializer<?> getDefaultRedisSerializer() {
        return new JdkSerializationRedisSerializer();
    }
}
